<template>
  <div class="message-content">
    <div ref="textRef" class="leading-relaxed break-words">
      <div class="markdown-body" v-html="text"></div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, watch } from "vue";
import MarkdownIt from "markdown-it";
import mila from "markdown-it-link-attributes";
import mdKatex from "@traptitech/markdown-it-katex";
import hljs from "highlight.js";

const props = defineProps({
  textInfo: {
    type: String,
    default: "",
  },
});

const textRef = ref(null);

// 创建 markdown-it 实例
const mdi = new MarkdownIt({
  html: true,
  linkify: true,
  highlight(code, language) {
    const validLang = hljs.getLanguage(language);
    if (validLang) {
      const lang = language || "";
      return `<div class="code-block-wrapper">
                <button class="copy-button">复制</button>
                <pre><code class="hljs ${lang}">${
        hljs.highlight(code, { language: lang }).value
      }</code></pre>
              </div>`;
    }
    return `<div class="code-block-wrapper">
              <button class="copy-button">复制</button>
              <pre><code class="hljs">${
                hljs.highlightAuto(code).value
              }</code></pre>
            </div>`;
  },
});

// 配置 markdown-it 插件
mdi.use(mila, { attrs: { target: "_blank", rel: "noopener" } });
mdi.use(mdKatex, {
  blockClass: "katexmath-block rounded-md p-[10px]",
  errorColor: " #cc0000",
});

// 计算处理后的文本
const text = computed(() => {
  const value = props.textInfo.replaceAll("*", "") || "";
  return mdi.render(value);
});

// 设置代码块复制功能
const setupCodeCopy = () => {
  const codeBlocks = textRef.value?.querySelectorAll(".code-block-wrapper");
  if (!codeBlocks) return;

  codeBlocks.forEach((wrapper) => {
    const copyButton = wrapper.querySelector(".copy-button");
    const codeElement = wrapper.querySelector("code");

    if (!copyButton || !codeElement) return;

    copyButton.addEventListener("click", async () => {
      try {
        await navigator.clipboard.writeText(codeElement.textContent || "");
        copyButton.textContent = "已复制!";
        copyButton.classList.add("copied");

        setTimeout(() => {
          copyButton.textContent = "复制";
          copyButton.classList.remove("copied");
        }, 3000);
      } catch (err) {
        console.error("复制失败:", err);
      }
    });
  });
};

// 监听内容变化
watch(
  () => props.textInfo,
  () => {
    setTimeout(setupCodeCopy, 0);
  },
  { flush: "post" }
);

// 组件挂载时初始化
onMounted(() => {
  setTimeout(setupCodeCopy, 0);
});
</script>

<style lang="less">
.message-content {
  border-radius: 10px;
  margin-left: 10px;
  min-width: 40px;
  padding: 10px;
  color: white;
  position: relative;

  // 已有的样式保持不变
  &.user,
  &.assistant {
    // ... 现有样式 ...
  }

  // 添加代码块相关样式
  .code-block-wrapper {
    position: relative;
    margin: 1em 0;

    pre {
      margin: 0;
      padding: 1em;
      border-radius: 6px;
      background-color: #282c34;
      overflow-x: auto;
    }

    .copy-button {
      position: absolute;
      top: 8px;
      right: 8px;
      padding: 4px 8px;
      font-size: 12px;
      color: #ffffff;
      background-color: rgba(255, 255, 255, 0.1);
      border: 1px solid rgba(255, 255, 255, 0.2);
      border-radius: 4px;
      cursor: pointer;
      opacity: 0;
      transition: all 0.2s ease;
      z-index: 10;

      &:hover {
        background-color: rgba(255, 255, 255, 0.2);
      }

      &.copied {
        background-color: #4caf50;
        border-color: #4caf50;
        opacity: 1;
      }
    }

    &:hover {
      .copy-button {
        opacity: 1;
      }
    }
  }

  // 代码高亮样式调整
  .hljs {
    color: #abb2bf;
    background: transparent;
    font-family: "Courier New", Courier, monospace;
    font-size: 0.9em;
    line-height: 1.5;
  }
}

// 针对不同角色消息的代码块样式调整
.user .message-content {
  .code-block-wrapper {
    pre {
      background-color: rgba(0, 0, 0, 0.2);
    }
  }
}

.assistant .message-content {
  .code-block-wrapper {
    pre {
      background-color: #282c34;
    }
  }
}
</style> 